<template lang="pug">
input.hidden(v-el:input, type="file", v-model="file")
</template>

<script>
import qiniu from '../../helper/qiniu'

export default {
  name: 'uploadInput',
  props: ['value'],
  data () {
    return {
      file: null,
    }
  },
  watch: {
    file: {
      handler (val, old) {
        const files = this.$els.input.files
        if (_.isEmpty(files)) {
          return
        }
        catchError(async () => {
          const { key } = await qiniu.upload(files[0])
          this.value = qiniu.url(key)
          this.file = null
        }, '上传成功', e => {
          this.file = null
          notieError(e)
        })
      },
      deep: true,
    },
  },
}
</script>

<style lang="sass" scoped>
</style>
